package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.blog.common.BlogException;
import com.example.blog.common.Utils.JWTUtils;
import com.example.blog.common.Utils.SecurityUtil;
import com.example.blog.data.BlogInfo;
import com.example.blog.data.UserInfo;
import com.example.blog.data.UserInfoPram;
import com.example.blog.pojo.resquest.UserLoginRequest;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.pojo.response.UserInfoResponse;
import com.example.blog.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static com.example.blog.constant.Constant.NO_DATELEFLAG;
import static com.example.blog.constant.Constant.UNLOGIN;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Override
    public  UserLoginRequest login(UserInfoPram user) {
        QueryWrapper<UserInfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());
        queryWrapper.eq("delete_flag",NO_DATELEFLAG);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        if(userInfo==null||userInfo.getId()<1){
            throw new BlogException("用户不存在");
        }

        SecurityUtil securityUtil=new SecurityUtil();
        if(securityUtil.verify(user.getPassword(), user.getPassword())){
            throw new BlogException("用户密码错误");
        }
//        if(!userInfo.getPassword().equals(user.getPassword())){
//            throw new BlogException("用户密码错误");
//        }
        Map<String,Object>map=new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",userInfo.getUserName());
        String jwt = JWTUtils.jwt(map);
        return new UserLoginRequest(userInfo.getId(),jwt);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
      UserInfo userInfo=idGetUserInfo(userId);
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        BeanUtils.copyProperties(userInfo,userInfoResponse);
        return userInfoResponse;
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = idGetBlogInfo(blogId);
        Integer userId=blogInfo.getUserId();
        return getUserInfo(userId);
    }
    public BlogInfo idGetBlogInfo(Integer blogId){
       return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId,blogId)
                .eq(BlogInfo::getDeleteFlag,UNLOGIN)
        );
    }


    public UserInfo idGetUserInfo(Integer userId){
        QueryWrapper<UserInfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        queryWrapper.eq("delete_flag",NO_DATELEFLAG);
        return userInfoMapper.selectOne(queryWrapper);
    }
}
